﻿using System.Reflection;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.JSInterop;

namespace SageKing.Blazor.OpenIdService.Components.Base;

public interface IAComponentBase<T>
{
    #region 通用高参数

    /// <summary>
    ///  Header + Tab height
    /// 顶部偏移高度，默认 64 + 50 = 114
    /// </summary>
    public double TopOffset { get; set; }
    /// <summary>
    ///  分页组件高度
    /// </summary>
    public double PaginationHeight { get; set; }
    /// <summary>
    /// 滚动条高度
    /// </summary>
    public double ScrollBarHeight { get; set; }

    /// <summary>
    /// 通用工具栏高度
    /// </summary>
    public double IndexToolBarHeight { get; set; }

    /// <summary>
    /// 表格工具栏顶部高度
    /// </summary>
    public double TableToolBarHeight { get; set; }

    /// <summary>
    /// 分割条宽度
    /// </summary>
    public double SplitterWidth { get; set; }
    #endregion

    #region 通用参数

    [Inject]
    public NavigationManager NavigationManager { get; set; }

    [Inject]
    public AntDesign.IMessageService Message { get; set; }

    [Inject]
    protected IConfirmService ConfirmService { get; set; }

    [Inject]
    public ProtectedSessionStorage ProtectedSessionStore { get; set; }

    [Inject]
    protected ILogger<T> Logger { get; set; }

    [Inject]
    public NotificationService Notice { get; set; }

    [Inject]
    public IJSRuntime Js { get; set; }

    [Inject]
    public JsCommJsInterop JsCommModule { get; set; }

    #endregion

    #region copy
    public bool ShowManualCopy { get; set; }

    /// <summary>
    /// Copy to clipboard with permission check and fallback
    /// </summary>
    /// <param name="copy"></param>
    /// <returns></returns>
    public async Task IClipboard(ElementReference copyRef, string copy)
    {

        // 尝试直接复制
        string StatusMessageCopy = "尝试直接复制";
        try
        {
            // 检查浏览器是否支持必要的 APIs
            var isSupported = await JsCommModule.CheckClipboardSupport();
            if (!isSupported)
            {
                StatusMessageCopy = "您的浏览器不支持剪贴板API。";
            }
            else
            {
                bool success = await JsCommModule.BlazorClipboardCopy(copyRef, copy);
                if (success)
                {
                    StatusMessageCopy = "复制成功！";
                    _ = Message.SuccessAsync(StatusMessageCopy);
                    return;
                }
                else
                {
                    StatusMessageCopy = "复制失败！";
                }
            }
        }
        catch (Exception ex) // 捕获权限拒绝或其他错误
        {
            StatusMessageCopy = $"复制错误: {ex.Message}";
            Console.WriteLine(StatusMessageCopy);
        }

        // 如果复制失败或权限被拒绝，降级到手动复制 
        _ = Message.WarningAsync(StatusMessageCopy);
    }

    public async Task ShowFallbackUI(ElementReference fallbackTextArea, string msg)
    {
        // 将文本设置到隐藏的 textarea 并选中 
        await JsCommModule.SetTextAndSelect(fallbackTextArea, msg);
        ShowManualCopy = true;
    }
    #endregion

    #region 共用方法 
    /// <summary>
    /// 通用实现反射调用泛型方法
    /// </summary>
    /// <param name="me"></param>
    /// <param name="targetType"></param>
    /// <param name="methodName"></param>
    /// <param name="parms"></param>
    /// <returns></returns>
    public object IInvokeGenericMethod(object me, Type targetType, string methodName, params object[] parms)
    {
        var method = me.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
        var genericMethod = method.MakeGenericMethod(targetType);
        var result = genericMethod.Invoke(me, parms);
        return result;
    }
    #endregion
}
